import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home
  },
  {
    path: '/category',
    name: 'category',
    meta:{
      hideHeader:true
    },
    component: () => import('../views/Category.vue')
  },{
    path:'/cart',
    name:'cart',
    meta:{
      hideHeader:true
    },
    component: () => import('../views/Cart.vue')
  },{
    path:'/admin',
    name:'admin',
    meta:{
      hideHeader:true
    },
    component: () => import('../views/Admin.vue')
  },{
    path:'/register',
    name:'register',
    component: () => import('../views/register/register.vue')
  },{
    path:'/popup',
    name:'popup',
    meta:{
      hideHeader:true,
      hideTabber:true,
    },
    component: ()=>import('../views/Popup.vue')
  },{
    path:'/user',
    name:'user',
    meta:{
      hideTabber:true
    },
    component:()=>import('../views/User.vue')
  },{
    path:'/productDetail',
    name:'productDetail',
    meta:{
      hideTabber:true,
    },
    component: ()=> import('../views/ProductDetail.vue')
  },{
    path:'/recharge',
    name:'recharge',
    meta:{
      hideTabber:true
    },
    component:()=>import('../views/Recharge.vue')
  },{
    path:'/collect',
    name:'collect',
    meta:{
      hideTabber:true
    },
    component:()=>import('../views/Collect.vue')
  },{
    path:'/address',
    name:'address',
    meta:{
      hideTabber:true
    },
    component:()=>import('../views/address/Address.vue')
  },{
    path:'/addressEdit',
    name:'addressEdit',
    meta:{
      hideTabber:true
    },
    component:()=>import('../views/address/AddressEdit.vue')
  },{
    path:'/order',
    name:'order',
    meta:{
      hideTabber:true
    },
    component:()=>import('../views/Order.vue')
  },{
    path:'/comment',
    name:'comment',
    meta:{
      hideTabber:true
    },
    component:()=>import('../views/Comment.vue')
  }
]

const router = new VueRouter({
  routes
})

import { Notify } from 'vant';

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err);
}

router.beforeEach((to,from,next)=>{
  let token = localStorage.getItem('token')
  if(['cart','collect','address'].includes(to.name)){
    if(token) next()
    else{
      Notify({ type: 'warning', message: '您还未登录！' });
    }
  }else next()
})

export default router
